package com.yz.vi.commons.result;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: yutingting
 * @create: 2021-01-22 09:59
 **/
public class ErrorContext implements Serializable {
    private static final long serialVersionUID = 5681392756896177777L;
    private List<CommonError> errorStack = new ArrayList();
    private String thirdPartyError;
    private static final String SPLIT = "|";

    public ErrorContext() {
    }

    public CommonError fetchCurrentError() {
        return this.errorStack != null && this.errorStack.size() > 0 ? (CommonError)this.errorStack.get(this.errorStack.size() - 1) : null;
    }

    public String fetchCurrentErrorCode() {
        return this.errorStack != null && this.errorStack.size() > 0 ? ((CommonError)this.errorStack.get(this.errorStack.size() - 1)).getErrorCode() : null;
    }

    public String fetchCurrentErrorMsg() {
        return this.errorStack != null && this.errorStack.size() > 0 ? ((CommonError)this.errorStack.get(this.errorStack.size() - 1)).getErrorMsg() : null;
    }

    public CommonError fetchRootError() {
        return this.errorStack != null && this.errorStack.size() > 0 ? (CommonError)this.errorStack.get(0) : null;
    }

    public void addError(CommonError error) {
        if (this.errorStack == null) {
            this.errorStack = new ArrayList();
        }

        this.errorStack.add(error);
    }

    public String toDigest() {
        StringBuffer sb = new StringBuffer();

        for(int i = this.errorStack.size(); i > 0; --i) {
            if (i == this.errorStack.size()) {
                sb.append(this.digest((CommonError)this.errorStack.get(i - 1)));
            } else {
                sb.append("|").append(this.digest((CommonError)this.errorStack.get(i - 1)));
            }
        }

        return sb.toString();
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();

        for(int i = this.errorStack.size(); i > 0; --i) {
            if (i == this.errorStack.size()) {
                sb.append(this.errorStack.get(i - 1));
            } else {
                sb.append("|").append(this.errorStack.get(i - 1));
            }
        }

        return sb.toString();
    }

    private String digest(CommonError commonError) {
        return null == commonError ? null : commonError.toDigest();
    }

    public List<CommonError> getErrorStack() {
        return this.errorStack;
    }

    public void setErrorStack(List<CommonError> errorStack) {
        this.errorStack = errorStack;
    }

    public String getThirdPartyError() {
        return this.thirdPartyError;
    }

    public void setThirdPartyError(String thirdPartyError) {
        this.thirdPartyError = thirdPartyError;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof ErrorContext)) {
            return false;
        } else {
            ErrorContext other = (ErrorContext)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$errorStack = this.getErrorStack();
                Object other$errorStack = other.getErrorStack();
                if (this$errorStack == null) {
                    if (other$errorStack != null) {
                        return false;
                    }
                } else if (!this$errorStack.equals(other$errorStack)) {
                    return false;
                }

                Object this$thirdPartyError = this.getThirdPartyError();
                Object other$thirdPartyError = other.getThirdPartyError();
                if (this$thirdPartyError == null) {
                    if (other$thirdPartyError != null) {
                        return false;
                    }
                } else if (!this$thirdPartyError.equals(other$thirdPartyError)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof ErrorContext;
    }

    public int hashCode() {
        int result = 1;
        Object $errorStack = this.getErrorStack();
        result = result * 59 + ($errorStack == null ? 43 : $errorStack.hashCode());
        Object $thirdPartyError = this.getThirdPartyError();
        result = result * 59 + ($thirdPartyError == null ? 43 : $thirdPartyError.hashCode());
        return result;
    }
}
